package org.zeuzgroup.console;

import java.awt.Color;
import java.awt.Point;
import java.awt.Robot;

import org.slf4j.Logger;

public class Utils {
	static final Logger log = org.slf4j.LoggerFactory.getLogger(Utils.class);

	public static boolean testAreUsable(Robot robot) {

		if (testAreUsableRunOnce(robot)) {
			return true;
		} else {
			for (int i = 0; i < 10; i++) {
				robot.delay(3000);
				if (testAreUsable(robot)) {
					return true;
				}
			}
		}
		return false;

	}

	public static boolean debugAreUsable(Robot robot) {

		for (int i = 0; i < 100; i++) {
			robot.delay(3000);
			testAreUsable(robot);
		}

		return false;
	}

	private static boolean testAreUsableRunOnce(Robot robot) {
		
		Point oldLocation = java.awt.MouseInfo.getPointerInfo()
		.getLocation();

		
		Point moveTo = new Point(400, 400);
		robot.mouseMove(moveTo.x, moveTo.y);
		Point currentLocation = java.awt.MouseInfo.getPointerInfo()
				.getLocation();
		robot.mouseMove(oldLocation.x,oldLocation.y);
		log.info("Could move mouse:" + moveTo.equals(currentLocation));
		Color c = robot.getPixelColor(moveTo.x, moveTo.y);
		log.info("Could get Colors:" + c.toString());

		return moveTo.equals(currentLocation)
				|| (c.getRed() != 0 || c.getBlue() != 0 || c.getGreen() != 0);

	}

	/**
	 * takes two colors and returns true of they are within the offset
	 * 
	 * @param color
	 * @param color2
	 * @param offSet
	 * @return
	 */
	public static boolean isWithinOffSet(Color color, Color color2, int offSet) {
		
		int r = 0, g = 0, b = 0;
		r = Math.abs(color.getRed() - color2.getRed());
		g = Math.abs(color.getGreen() - color2.getGreen());
		b = Math.abs(color.getBlue() - color2.getBlue());
		if (r < offSet && g < offSet && b < offSet) {
			log.info("Color was in range");
			return true;
		}
		return false;

	}
	public static void keyHitAndRelease(Robot robot, int event) {
		robot.keyPress(event);
		robot.delay(500);
		robot.keyRelease(event);
	}
	public static void keyHitAndRelease(Robot robot, int event, int duration) {
		robot.keyPress(event);
		robot.delay(duration);
		robot.keyRelease(event);
	}

	
	public static void mouseClickAndRelease(Robot robot, int event) {
		robot.mousePress(event);
		robot.delay(500);
		robot.mouseRelease(event);
		robot.delay(500);
	}


}
